package com.cecsmsserve.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cecsmsserve.entity.HealthReminder;
import com.cecsmsserve.service.HealthReminderService;
import com.cecsmsserve.utils.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/health/reminder")
public class HealthReminderController {

    @Autowired
    private HealthReminderService healthReminderService;

    /**
     * 添加健康提醒
     */
    @PostMapping("")
    public CommonResult<Boolean> addReminder(@RequestBody HealthReminder healthReminder) {
        if (healthReminder.getUserId() == null || healthReminder.getTypeId() == null || 
            healthReminder.getReminderTime() == null || healthReminder.getFrequency() == null) {
            return CommonResult.validateFailed("缺少必要参数");
        }
        
        boolean success = healthReminderService.addReminder(healthReminder);
        return success ? CommonResult.success(true) : CommonResult.failed("添加提醒失败");
    }
    
    /**
     * 分页获取用户健康提醒
     */
    @GetMapping("/{userId}")
    public CommonResult<Page<HealthReminder>> getReminders(
            @PathVariable Integer userId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<HealthReminder> page = healthReminderService.getRemindersByUserIdWithPage(pageNum, pageSize, userId);
        return CommonResult.success(page);
    }
    
    /**
     * 更新健康提醒
     */
    @PutMapping("")
    public CommonResult<Boolean> updateReminder(@RequestBody HealthReminder healthReminder) {
        if (healthReminder.getId() == null || healthReminder.getUserId() == null) {
            return CommonResult.validateFailed("缺少必要参数");
        }
        
        boolean success = healthReminderService.updateReminder(healthReminder);
        return success ? CommonResult.success(true) : CommonResult.failed("更新提醒失败");
    }
    
    /**
     * 删除健康提醒
     */
    @DeleteMapping("/{id}")
    public CommonResult<Boolean> deleteReminder(@PathVariable Integer id, @RequestParam Integer userId) {
        boolean success = healthReminderService.deleteReminder(id, userId);
        return success ? CommonResult.success(true) : CommonResult.failed("删除提醒失败");
    }
    
    /**
     * 启用/禁用健康提醒
     */
    @PutMapping("/status")
    public CommonResult<Boolean> updateReminderStatus(
            @RequestParam Integer id,
            @RequestParam Integer userId,
            @RequestParam Boolean isActive) {
        boolean success = healthReminderService.updateReminderStatus(id, userId, isActive);
        return success ? CommonResult.success(true) : CommonResult.failed("更新提醒状态失败");
    }
} 